<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Request;

class Answer extends Model
{
		/* 添加用户回答API */
    public function add()
    {
    	/* 检查用户是否登录 */
    	if (!user_ins()->is_logged_in())
    		return ['status' => 0, 'msg' => 'Login Required'];

    	/* 检查回答是否有question_id和content */
    	if (!Request::get('question_id') || !Request::get('content'))
    		return ['status' => 0, 'msg' => 'Question_id and Content Required'];

    	/* 获取参数对应的Model,检查问题是否存在(判断是否存在该question实例) */
    	$question = question_ins()->find(Request::get('question_id'));
  		if (!$question)
  			return ['status' => 0, 'msg' => 'Question Not Exists'];

  		/* 不允许往数据表插入重复数据(不允许重复灌水评论) */
  		$is_answer = $this->where(['question_id' => Request::get('question_id'), 'user_id' => session('user_id'), 'content' => Request::get('content')])
  		->count();
  		if ($is_answer)
  			return ['status' => 0, 'msg' => 'Duplicate Answers'];

  		/* 设置数据表新的记录 */
  		$this->content = Request::get('content');
  		$this->question_id = Request::get('question_id');
  		$this->user_id = session('user_id');

  		/* 判断保存，插入数据库 */
  		return $this->save()?
  		['status' => 1, 'id' => $this->id]:
  		['status' => 0, 'msg' => 'DB insert Failed'];
    }

		/* 更新用户回答API */
    public function change()
    {
    	/* 检查用户是否登录 */
    	if (!user_ins()->is_logged_in())
    		return ['status' => 0, 'msg' => 'Login Required'];

    	/* 是否传递了id,存在则获取对应Model */
    	if (!Request::get('id'))
    		return ['status' => 0, 'msg' => 'id is Required'];

    	$answer = $this->find(Request::get('id'));

    	/* 数据表中是否存在这个传递过来的参数id */
    	if (!$answer)
    		return ['status' => 0, 'msg' => 'No This Answer id'];
    	/* 检查权限：回复者是否是这个更新者 */
    	if ($answer->user_id != session('user_id'))
    		return ['status' => 0, 'msg' => 'Permission Denued'];

    	/* 更新内容 */
    	$answer->content = Request::get('content');
    	$answer->save()?
    	['status' => 1]:
    	['status' => 0, 'msg' => 'DB Update Failed'];

    }

    /* 查看回答API */
    public function read()
    {
    	/* 未传递question_id或answers的id(id找一条回复,question_id找所有回复) */
    	if (!Request::get('id') && !Request::get('question_id'))
    		return ['status' => 0, 'msg' => 'id or Question-id Required'];

    	/* 根据id是否存在查看单个回答 */
    	if (Request::get('id'))
    	{
    		/* 获取对应Model,判断该回复是否存在 */
    		$answer = $this->find(Request::get('id'));
    		if (!$answer)
    			return ['status' => 0, 'msg' => 'Answer Not Exists'];
    		return ['status' => 0, 'data' => $answer];
    	}

    	/* 判断该问题是否存在 */
    	if (!question_ins()->find(Request::get('question_id')))
    		return ['status' => 0, 'msg' => 'Question Not Exists'];

			/*查看同一问题下所有回答 
			 *在查看回答前检查问题question_id是否存在,前端需要做JSON数组非空验证
			 */
			$answer = $this->where('question_id', Request::get('question_id'))
			->get();

    	/* 参数id和question_id都存在，则返回对应数据 */
    	return ['status' => 1, 'data' => $answer];
    }

    /* 跨表查询的通用API,Laravel多对多跨表查询神器：中间表Pivot */
    public function user()
    {
      /* 手动添加的字段需要注册 */
      return $this
      ->belongsToMany('App\User')
      ->withPivot('vote')
      ->withTimestamps();
    }

    public function vote()
    {
      return 1;
    }


}
